package com.example.nophase;



import android.content.DialogInterface;
import android.content.Intent;

import android.os.Bundle;

import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ProgressBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.example.nophase.api.RequstCommonApi;
import com.example.nophase.common.BaseCallBack;
import com.example.nophase.common.LoginManager;
import com.example.nophase.entity.MoreInfo;
import com.example.nophase.entity.SysUserVO;
import com.example.nophase.entity.TokenVO;
import com.example.nophase.utils.HttpUtils;
import com.example.nophase.utils.SPUtils;
import com.example.nophase.utils.Sha1Utils;
import com.example.nophase.utils.V;
import com.google.gson.Gson;
import com.rengwuxian.materialedittext.MaterialEditText;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.account_et_login)
    MaterialEditText accountEtLogin;
    @BindView(R.id.password_et_login)
    MaterialEditText passwordEtLogin;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.login_progressbar)
    ProgressBar loginProgressbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
        ButterKnife.bind(this);
        loginProgressbar.setVisibility(View.GONE);
        Long unlogintimer = (Long) SPUtils.get(this, "unlogintimer", 0L);
        if (unlogintimer <= 0L) {
            SPUtils.put(this, "unlogintimer", new Date().getTime());
        }
        autoInjectAccountInfo();

        SPUtils.put(this, "text", "hello");
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = accountEtLogin.getText().toString();
                String password = passwordEtLogin.getText().toString();

                loginProgressbar.setVisibility(View.VISIBLE);
                Call<TokenVO> call = HttpUtils.getInstance().getApiService(RequstCommonApi.class).appLogin(username, Sha1Utils.encry(password));
                call.enqueue(new BaseCallBack<TokenVO>() {
                    @Override
                    public void onSuccess(Response<TokenVO> response) throws Exception {
                        loginProgressbar.setVisibility(View.GONE);
                        SPUtils.put(LoginActivity.this, "unlogintimer", -1L);
                        String acctounInfo = accountEtLogin.getText().toString() + "," + passwordEtLogin.getText().toString();
                        SPUtils.put(LoginActivity.this, "account", acctounInfo);
                        Gson gson = new Gson();
                        SysUserVO sysUserVO = gson.fromJson(response.body().getData().toString(), SysUserVO.class);
                        LoginManager.setUserInfo(LoginActivity.this, sysUserVO);
                        LoginManager.setToken(LoginActivity.this, response.body().getAccess_token());
                        HttpUtils.getInstance().setAuthToken(response.body().getAccess_token());
                        if (response.body().getData().getRoles() != null) {
                            MoreInfo moreInfo = response.body().getData().getMoreInfo();
                            String roles = moreInfo.getPosition();


                            SPUtils.put(LoginActivity.this, "password", password);
                            startActivity(new Intent(LoginActivity.this, HomeActivity.class));

                        }

                    }

                    @Override
                    public void onFailure(String message) {
                        loginProgressbar.setVisibility(View.GONE);

                        AlertDialog dialog = new AlertDialog.Builder(LoginActivity.this)
                                .setTitle("提示")//提示框标题
                                .setIcon(R.drawable.apply_for_from)
                                .setMessage(message+",请联系管理员")
                                .setPositiveButton("关闭",
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                               dialog.dismiss();
                                            }
                                        }).create();
                        dialog.setCanceledOnTouchOutside(false);
                        dialog.show();
                        new Timer().schedule(new TimerTask() {
                            public void run() {
                                dialog.dismiss();
                            }
                        }, 3000);


                    }
                });

            }
        });

    }

    private void autoInjectAccountInfo() {
        String acctountInfo = (String) SPUtils.get(this, "account", "");
        if (!V.isEmpty(acctountInfo)) {
            String[] array = acctountInfo.split(",");
            accountEtLogin.setText(array[0]);
            passwordEtLogin.setText(array[1]);
        }
    }



}
